<?php
declare (strict_types=1);

namespace app\controller;

use app\model\PeerGroup as PeerGroupModel;
use app\model\GroupMember as GroupMemberModel;
use think\facade\Request;
use think\facade\Validate;

class PeerGroup
{
    public function createGroup()
    {
        $data = Request::post();

        // 验证数据
        $validate = Validate::rule([
            'group_name' => 'require|max:255',
        ]);

        if (!$validate->check($data)) {
            return json(['error' => $validate->getError()], 400);
        }

        // 创建小组
        $group = new PeerGroupModel();
        $group->group_name = $data['group_name'];
        $group->save();

        // 创建小组成员记录 (创建者作为管理员)
        $member = new GroupMemberModel();
        $member->group_id = $group->group_id;
        $member->user_id = $data['user_id'];  // 从登录状态获取
        $member->role = 'admin';
        $member->save();

        return json(['message' => '小组创建成功', 'group_id' => $group->group_id]);
    }

    public function joinGroup()
    {
        $data = Request::post();

        // 验证数据
        $validate = Validate::rule([
            'group_id' => 'require|integer',
            'user_id' => 'require|integer',
        ]);

        if (!$validate->check($data)) {
            return json(['error' => $validate->getError()], 400);
        }

        // 加入小组
        $member = new GroupMemberModel();
        $member->group_id = $data['group_id'];
        $member->user_id = $data['user_id'];
        $member->role = 'member';
        $member->save();

        return json(['message' => '加入小组成功', 'group_member_id' => $member->member_id]);
    }

    // 更新小组信息
    public function update()
    {
        $data = Request::post();

        $validate = Validate::rule([
            'group_id' => 'require|integer',
            'name' => 'require|max:100',
            'description' => 'max:500',
        ]);

        if (!$validate->check($data)) {
            return json(['status' => 'error', 'message' => $validate->getError()]);
        }

        $group = PeerGroupModel::find($data['group_id']);
        if (!$group) {
            return json(['status' => 'error', 'message' => 'Group not found']);
        }

        $group->name = $data['name'];
        $group->description = $data['description'] ?? $group->description;
        $group->save();

        return json(['status' => 'success', 'message' => 'Group updated successfully']);
    }

    // 退出小组
    public function leave()
    {
        $data = Request::post();

        $validate = Validate::rule([
            'group_id' => 'require|integer',
            'user_id' => 'require|integer',
        ]);

        if (!$validate->check($data)) {
            return json(['status' => 'error', 'message' => $validate->getError()]);
        }

        $member = GroupMemberModel::where('group_id', $data['group_id'])
            ->where('user_id', $data['user_id'])
            ->find();
        if (!$member) {
            return json(['status' => 'error', 'message' => 'Group membership not found']);
        }

        $member->delete();

        return json(['status' => 'success', 'message' => 'Left group successfully']);
    }
}
